﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace KeyboardImageViewer
{
    /// <summary>
    /// Interaction logic for TagRenameOrDelete.xaml
    /// </summary>
    public partial class TagRenameOrDelete : Window
    {
        public TagRenameOrDelete()
        {
            InitializeComponent();
            ImageManager.Config.ApplyWindowSize("TagRenameOrDelete", this);

            this.UpdateTagList(string.Empty);
        }

        private void UpdateTagList()
        {
            this.UpdateTagList(this.searchBox.Text.ToLowerInvariant());
        }

        private void UpdateTagList(string searchString)
        {
            Dictionary<string, int> tagsSummary = Database.GetTagsSummary(ImageManager.Connection);

            if (searchString == string.Empty)
            {
                var selectedTags = from pair in tagsSummary orderby pair.Value descending select new TagWithFrequency { Tag = pair.Key, Frequency = pair.Value };
                this.tagBox.ItemsSource = selectedTags;
            }
            else
            {
                var selectedTags = from pair in tagsSummary where pair.Key.Contains(searchString) orderby pair.Value descending select new TagWithFrequency { Tag = pair.Key, Frequency = pair.Value };
                this.tagBox.ItemsSource = selectedTags;
            }
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            ImageManager.Config.SaveWindowSize("TagRenameOrDelete", this);
        }

        private void searchBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.UpdateTagList();
        }

        private void tagBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.tagBox.SelectedIndex >= 0)
            {
                this.renameButton.IsEnabled = true;
                this.deleteButton.IsEnabled = true;
            }
            else
            {
                this.renameButton.IsEnabled = false;
                this.deleteButton.IsEnabled = false;
            }
        }

        private void renameButton_Click(object sender, RoutedEventArgs e)
        {
            string oldTag = ((TagWithFrequency)this.tagBox.SelectedItem).Tag;
            string newTag = this.renameBox.Text.ToLowerInvariant();

            if (newTag.Contains(' ') || newTag.Contains('|'))
            {
                MessageBox.Show("Cannot rename tag. Tag cannot contain ' ' or '|' characters.");
                return;
            }

            if (newTag == string.Empty)
            {
                MessageBox.Show("New tag cannot be blank.");
                return;
            }

            if (MessageBox.Show("Are you sure you want to rename this tag?", "Confirm rename", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                TagOperations.RenameTag(oldTag, newTag);
                this.UpdateTagList();
            }
        }

        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            string tagToDelete = ((TagWithFrequency)this.tagBox.SelectedItem).Tag;

            if (MessageBox.Show("Are you sure you want to remove this tag from all of your images?", "Confirm delete", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                TagOperations.DeleteTag(tagToDelete);
                this.UpdateTagList();
            }
        }
    }
}
